﻿using System;
using InfoClasses;
using DbControl;
using TextFunctions;


namespace NICE_ToursBD
{  
    class Program
    {
        static void Main(string[] args)
        {
            DBControl dBControll = new DBControl();
            ConsoleKeyInfo _key = new ConsoleKeyInfo();
            TouristsInfo TouristsI = new TouristsInfo();
            ToursInfo ToursI = new ToursInfo();
            Console.WriteLine("This program demonstrates the basic functions for working with databases.");
            #region
            for (; ; )
            {
                Console.WriteLine(" \n                                 *MAIN MENU* \n" +
                "\nTo select action - enter the number and press Enter \n" +
                "\n1. Adding a new record           (Button A)" +
                "\n2. Updating a record             (Button U)" +
                "\n3. Deleting a record             (Button D)" +
                "\n4. Show sorted table             (Button S)" +
                "\n5. Show specific info            (Button G)" +
                "\n6. Output to screen / file       (Button O)" +
                "\n7. Exit the program              (Button Q)\n");

            M: _key = Console.ReadKey(true);

                if (_key.Key == ConsoleKey.Q)
                {
                    break;
                }

                switch (_key.Key)
                {
                    case ConsoleKey.A:
                        {
                            #region
                            Console.WriteLine("\nAdding a new record: ");
                            for (; ; )
                            {
                                TextFunc.Select_Table();
                            L: _key = Console.ReadKey(true);
                                if (_key.Key == ConsoleKey.B)
                                {
                                    break;
                                }
                                switch (_key.Key)
                                {
                                    case ConsoleKey.D1:
                                        {
                                            bool Mark = false;
                                            ConsoleKeyInfo _key2 = new ConsoleKeyInfo();

                                            while (Mark == false)
                                            {
                                                Console.WriteLine("\nTo enter new information for adding press    (Buton A)" +
                                                    "\nTo see your input  press                     (Buton S)" +
                                                    "\nTo fix your input press                      (Buton F)" +
                                                    "\nTo confirm eding of new row press            (Buton Enter)" +
                                                    "\nTo end operation and go to previos menu      (Buton B)\n");
                                            K: _key2 = Console.ReadKey(true);

                                                switch (_key2.Key)
                                                {
                                                    case ConsoleKey.A:
                                                        {
                                                            TouristsI.ReadInfo();
                                                            break;
                                                        }
                                                    case ConsoleKey.S:
                                                        {
                                                            TouristsI.ShowInfo();
                                                            break;
                                                        }
                                                    case ConsoleKey.F:
                                                        {
                                                            TouristsI.FixInfo();
                                                            break;
                                                        }
                                                    case ConsoleKey.Enter:
                                                        {
                                                            dBControll.AddNewRow(TouristsI);
                                                            Console.WriteLine("New Row Added\n");
                                                            dBControll._dataSet.AcceptChanges();
                                                            break;
                                                        }
                                                    case ConsoleKey.B:
                                                        {
                                                            Mark = true;
                                                            dBControll._dataSet.RejectChanges();
                                                            break;
                                                        }
                                                    default:
                                                        {
                                                            Console.WriteLine("\n Program can not recognize the comand, try again!");
                                                            goto K;
                                                        }
                                                }
                                            }

                                            break;
                                        }
                                    case ConsoleKey.D2:
                                        {
                                            bool Mark = false;
                                            ConsoleKeyInfo _key2 = new ConsoleKeyInfo();

                                            while (Mark == false)
                                            {
                                                Console.WriteLine("\nTo enter new information for adding press    (Buton A)" +
                                                "\nTo see your input  press                     (Buton S)" +
                                                "\nTo fix your input press                      (Buton F)" +
                                                "\nTo confirm eding of new row press            (Buton Enter)" +
                                                "\nTo end operation and go to previos menu      (Buton B)\n");
                                            S: _key2 = Console.ReadKey(true);

                                                switch (_key2.Key)
                                                {
                                                    case ConsoleKey.A:
                                                        {
                                                            ToursI.ReadInfo();
                                                            break;
                                                        }
                                                    case ConsoleKey.S:
                                                        {
                                                            ToursI.ShowInfo();
                                                            break;
                                                        }
                                                    case ConsoleKey.F:
                                                        {
                                                            ToursI.FixInfo();
                                                            break;
                                                        }
                                                    case ConsoleKey.Enter:
                                                        {
                                                            dBControll.AddNewRow(ToursI);
                                                            Console.WriteLine("New Row Added\n");
                                                            dBControll._dataSet.AcceptChanges();
                                                            break;
                                                        }
                                                    case ConsoleKey.B:
                                                        {
                                                            Mark = true;
                                                            dBControll._dataSet.RejectChanges();
                                                            break;
                                                        }
                                                    default:
                                                        {
                                                            Console.WriteLine("\n Program can not recognize the comand, try again!");
                                                            goto S;
                                                        }
                                                }
                                            }

                                            break;
                                        }

                                    default:
                                        {
                                            TextFunc.Try_Again();
                                            goto L;
                                        }
                                }

                            }
                            break;
                        }
                            #endregion
                    case ConsoleKey.U:
                        {
                            #region
                            Console.WriteLine("\n Updating a record:");
                            int _choise = 0;
                            for (; ; )
                            {
                                TextFunc.Select_Table();
                            T: _key = Console.ReadKey(true);
                                if (_key.Key == ConsoleKey.B)
                                {
                                    break;
                                }
                                switch (_key.Key)
                                {
                                    case ConsoleKey.D1:
                                        {
                                            #region
                                            // Here i have to:
                                            //show all rows consists in table Tourists:
                                            Console.WriteLine("\n");
                                            dBControll.ShowTable(TouristsI);
                                            //ask ID to update: 
                                            Console.WriteLine("\nHere is the contents of table Tourists." +
                                            "\nSelect the row to update by typing its number. \n");
                                            //read ID from keyboard, check it for correctness

                                            try
                                            {
                                                _choise = (int)(Convert.ToInt32(Console.ReadLine()));
                                            }

                                            catch (Exception e)
                                            {
                                                Console.WriteLine(e.Message);
                                                Console.WriteLine("Try again");
                                            }

                                            Console.WriteLine("\n");
                                            //update chosen row:
                                            dBControll.Update("Tourists", _choise);
                                            //show result
                                            Console.WriteLine("\nHere is the result\n");
                                            dBControll.ShowTable(TouristsI);
                                            break;
                                            #endregion
                                        }

                                    case ConsoleKey.D2:
                                        {
                                            #region
                                            // Here i have to:
                                            //show all rows consists in table Tours:
                                            Console.WriteLine("\n");
                                            dBControll.ShowTable(ToursI);
                                            //ask ID to update: 
                                            Console.WriteLine("\nHere is the contents of table Tours." +
                                            "\nSelect the row to update by typing its number. \n");
                                            //read ID from keyboard, check it for correctness

                                            try
                                            {
                                                _choise = (int)(Convert.ToInt32(Console.ReadLine()));
                                            }

                                            catch (Exception e)
                                            {
                                                Console.WriteLine(e.Message);
                                                Console.WriteLine("Try again");
                                            }

                                            Console.WriteLine("\n");
                                            //update chosen row:
                                            dBControll.Update("Tours", _choise);
                                            //show result
                                            Console.WriteLine("\nHere is the result\n");
                                            dBControll.ShowTable(ToursI);
                                            break;
                                            #endregion
                                        }

                                    default:
                                        {
                                            #region
                                            TextFunc.Try_Again();
                                            goto T;
                                            #endregion
                                        }
                                }
                            }
                            break;
                            #endregion
                        }
                    case ConsoleKey.D:
                        {
                            #region
                            Console.WriteLine("\n Deleting a record:");
                            bool _mark = true;
                            int _choise = 0;
                            while (_mark == true)
                            {
                                TextFunc.Select_Table();
                            U: _key = Console.ReadKey(true);
                                switch (_key.Key)
                                {
                                    case ConsoleKey.D1:
                                        {
                                            #region
                                            // Here i have to:
                                            //show all rows consists in table Tourists:
                                            Console.WriteLine("\n");
                                            dBControll.ShowTable(TouristsI);
                                            //ask ID to delete: 
                                            Console.WriteLine("\nHere is the contents of table Tourists." +
                                            "\nSelect the row to delete by typing its id. \n");
                                            //read ID from keyboard, check it for correctness

                                            try
                                            {
                                                _choise = (int)(Convert.ToInt32(Console.ReadLine()));
                                            }

                                            catch (Exception e)
                                            {
                                                Console.WriteLine(e.Message);
                                                Console.WriteLine("Try again");
                                            }

                                            Console.WriteLine("\n");
                                            //delete chosen row:
                                            dBControll.DeleteRow("Tourists", _choise);
                                            //show result
                                            Console.WriteLine("\nHere is the result\n");
                                            dBControll.ShowTable(TouristsI);
                                            break;
                                            #endregion
                                        }
                                    case ConsoleKey.D2:
                                        {
                                            #region
                                            // Here i have to:
                                            //show all rows consists in table Tours:
                                            Console.WriteLine("\n");
                                            dBControll.ShowTable(ToursI);
                                            //ask ID to delete: 
                                            Console.WriteLine("\nHere is the contents of table Tours." +
                                            "\nSelect the row to delete by typing its id. \n");
                                            //read ID from keyboard, check it for correctness

                                            try
                                            {
                                                _choise = (int)(Convert.ToInt32(Console.ReadLine()));
                                            }

                                            catch (Exception e)
                                            {
                                                Console.WriteLine(e.Message);
                                                Console.WriteLine("Try again");
                                            }

                                            Console.WriteLine("\n");
                                            //delete chosen row:
                                            dBControll.DeleteRow("Tours", _choise);
                                            Console.WriteLine("\nHere is the result\n");
                                            dBControll.ShowTable(ToursI);
                                            break;
                                            #endregion
                                        }
                                    case ConsoleKey.B:
                                        {
                                            #region
                                            _mark = false;
                                            break;
                                            #endregion
                                        }
                                    default:
                                        {
                                            #region
                                            TextFunc.Try_Again();
                                            goto U;
                                            #endregion
                                        }

                                }
                            }
                            break;
                            #endregion
                        }
                    case ConsoleKey.S:
                        {
                            #region
                            Console.WriteLine("\n Show sorted information from d.b. sampling on parameters");
                            for (; ; )
                            {
                                TextFunc.Select_Table();
                            F: _key = Console.ReadKey(true);
                                if (_key.Key == ConsoleKey.B)
                                {
                                    break;
                                }
                                switch (_key.Key)
                                {
                                    case ConsoleKey.D1:
                                        {
                                            dBControll.FormOutput(TouristsI);
                                            dBControll.Sort(TouristsI, dBControll.Output);  
                                            break;
                                        }
                                    case ConsoleKey.D2:
                                        {
                                            dBControll.FormOutput(ToursI);
                                            dBControll.Sort(ToursI, dBControll.Output);
                                            break;
                                        }
                                    default:
                                        {
                                            TextFunc.Try_Again();
                                            goto F;
                                        }
                                }
                            }
                            break;
                            #endregion
                        }
                    case ConsoleKey.G:
                        {
                            #region
                            Console.WriteLine("\n Show some specific info");
                            dBControll.Group();
                            break;
                            #endregion
                        }
                    case ConsoleKey.O:
                        {
                            #region
                            Console.WriteLine("\n Output to screen / file");
                            for (; ; )
                            {
                                TextFunc.Select_Table();
                            Y: _key = Console.ReadKey(true);
                                if (_key.Key == ConsoleKey.B)
                                {
                                    break;
                                }
                                switch (_key.Key)
                                {
                                    case ConsoleKey.D1:
                                        {
                                            Console.WriteLine("\nShow Table in console press (Buton S)" +
                                            "\nPrint Table in to file press (Buton P)\n");
                                        R: _key = Console.ReadKey(true);
                                            Console.WriteLine();
                                            switch (_key.Key)
                                            {
                                                case ConsoleKey.S:
                                                    {
                                                        dBControll.ShowTable(TouristsI);
                                                        break;
                                                    }
                                                case ConsoleKey.P:
                                                    {
                                                        dBControll.PrintTable(TouristsI);
                                                        break;
                                                    }
                                                case ConsoleKey.B:
                                                    {
                                                        Console.WriteLine("\nTo end operation and go to previos menu      (Buton B)\n");
                                                        break;
                                                    }
                                                default:
                                                    {
                                                        TextFunc.Try_Again();
                                                        goto R;
                                                    }
                                            }

                                            break;

                                        }
                                    case ConsoleKey.D2:
                                        {
                                            Console.WriteLine("\nShow Table in console press (Buton S)" +
                                            "\nPrint Table in to file press (Buton P)");
                                        Z: _key = Console.ReadKey(true);
                                            switch (_key.Key)
                                            {
                                                case ConsoleKey.S:
                                                    {
                                                        dBControll.ShowTable(ToursI);
                                                        break;
                                                    }
                                                case ConsoleKey.P:
                                                    {
                                                        dBControll.PrintTable(ToursI);
                                                        break;
                                                    }
                                                case ConsoleKey.B:
                                                    {
                                                        Console.WriteLine("\nTo end operation and go to previos menu      (Buton B)\n");
                                                        break;
                                                    }
                                                default:
                                                    {
                                                        TextFunc.Try_Again();
                                                        goto Z;
                                                    }
                                            }

                                            break;
                                        }
                                    default:
                                        {
                                            TextFunc.Try_Again();
                                            goto Y;
                                        }
                                }
                            }
                            break;
                        }
                            #endregion
                    default:
                        {
                            #region
                            TextFunc.Try_Again();
                            goto M;
                            #endregion
                        }

                }
            #endregion
            }
            Console.WriteLine("\n\n                                   THE END\n Press Enter");
            Console.ReadKey(true);
        }
    }
}
